<template>
  <div class="mealsPlan">
    <div class="header">
      <div class="title">
        <img v-if="plan.kind == 1" src="../../../assets/icons/u.png" alt />
        <img v-if="plan.kind == 2&&plan.type == 1" src="../../../assets/icons/ci.png" alt />
        <img v-if="plan.kind == 2&&plan.type == 2" src="../../../assets/icons/ku.png" alt />
        <img v-if="plan.kind == 3" src="../../../assets/icons/kal.png" alt />
        {{plan.plan_title}}
      </div>
      <div class="nutrition">
        <span v-if="plan.carbohydrate">碳水:{{plan.carbohydrate}}g</span>
        <span v-if="plan.protein">蛋白质:{{plan.protein}}g</span>
        <span v-if="plan.fat">脂肪:{{plan.fat}}g</span>
        <Icon class="icon edit" @click="edit" type="ios-create-outline" />
        <Icon class="icon del" @click="del(plan.kind,plan.id)" type="ios-trash-outline" />
      </div>
    </div>
    <div class="foodList">
      <div class="foodItem" v-for="(item,index) in plan.detail" :key="index">
        <Tooltip class="tip" :content="item.title" placement="top-start"></Tooltip>
        <p class="name">{{item.title}}</p>
        <p class="weight">{{item.num}}{{item.unit}}</p>
      </div>
    </div>
    <div class="remark">
      备注：<Input search enter-button="确定" @on-search="addNote" style="width:300px" type="text" v-model="plan.note" placeholder="添加备注" maxlength="80" />
    </div>
  </div>
</template>
<script>
import {foodRecordDel,medicineRecordDel,sportsRecordDel,addNote} from '@/api/addPlan';
export default {
  props:{
    plan:{
      type:Object,
      default:{}
    }
  },
  data: function () {
    return {
      
    }
  },
  methods:{
    edit(){
      this.$emit('edit',this.plan)
    },
    del(kind,id){
      let res;
      this.$Modal.confirm({
        title: '确定删除？',
        onOk:async () => {
          if(kind == 1){
            //饮食
            res = await foodRecordDel({id})
          }else if(kind == 2){
            //用药
            res = await medicineRecordDel({id})
          }else if(kind == 3){
            //运动
            res = await sportsRecordDel({id})
          }
          if (res.code == 100000) {
            this.$Message.success(res.msg);
            this.$emit('del')
          } else {
            this.$Message.error(res.msg);
          }
        }
      });
      
    },
    addNote(){
      if(!this.plan.note){
        this.$Message.warning('请输入备注');
        return;
      }
      addNote({
        kind:this.plan.kind,
        id:this.plan.id,
        note:this.plan.note
      }).then(res=>{
        if(res.code == 100000){
          this.$Message.success(res.msg);
        }else{
          this.$Message.error(res.msg);
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.mealsPlan{
  box-sizing: border-box;
  padding: 15px 0 10px 10px;
  border-bottom: 1px solid #f4f4f4;
  .header{
    display: flex;
    align-items: center;
    justify-content: space-between;
    .title{
      display: flex;
      align-items: center;
      height: 20px;
      font-size: 13px;
      font-family: PingFang-SC-Heavy, PingFang-SC;
      font-weight: 800;
      color: #242424;
      img{
        width: 20px;
        height: 20px;
        margin-right: 8px;
      }
    }
    .nutrition{
      color: #9F9F9F;
      font-size: 12px;
      display: flex;
      align-items: center;
      span{
        margin-left: 20px;
      }
      .icon{
        font-size: 14px;
        color: #666;
        font-weight: bold;
        margin-left: 15px;
        cursor: pointer;
      }
      .del{
        margin-left: 10px;
      }
    }
  }
  .foodList{
    margin-top: 12px;
    display: flex;
    flex-wrap: wrap;
    .foodItem{
      width: 85px;
      height: 44px;
      border-radius: 5px;
      border: 1px solid #E6E9F0;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 12px;
      flex-wrap: wrap;
      position: relative;
      margin:0 18px 17px 0;
      .name{
        width: 80%;
        text-align: center;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
        color: #242424;
      }
      .weight{
        color: #9F9F9F;
      }
      .tip{
        width: 100%;
        height: 100%;
        position: absolute;
        right: 0px;
        top: 0px;
      }
    }
    .foodItem:nth-child(6n){
      margin-right: 0;
    }
  }
  .remark{
    font-size: 12px;
    font-family: PingFang-SC-Bold, PingFang-SC;
    font-weight: bold;
    color: #242424;
    display: flex;
    align-items: center;
  }
}
</style>
